import api from '~api/index'
import util from '~src/lib/util'
export default {
  state: {
    list: {
      data:[],
      page: 1,
      id:'-1',
      code:'0',
      total:0
    },
    show: {}
  },
  mutations: {
    ['setCardList'] (state, data) {
      if (data.code == '200'){
        if (data.page === 1) {
          data.data = [].concat(data.data)
          if (data.data.length === 0) {
            data.data = []
          }
        } else {
          data.data = state.list.data.concat(data.data)
        }
        state.list.data = data.data
      }
      state.list.code = data.code
    },
    ['setCardshow'] (state, show) {
      state.show = show
    },
    ['updataCardshow'] (state, id) {
      const data = state.list.data.find(ii => ii.card_id === id)
      state.show = data
    }
  },
  actions: {
    async ['getCardList'] ({commit}, config) {
      const { data: { data, code } } = await api.post('/v1/memberCardList',
        {openid: util.localItem.get('openid')})
      if (data && code === '200') {
        commit('setCardList', {
          data: data.cardList.length>0?data.cardList:[],
          page: config.page,
          total:data.cardList.length,
          code: code
        })
        config.callback && config.callback()
      }else{
        commit('setCardList', {
          data: [],
          page: config.page,
          code: code
        })
        config.stopCallback && config.stopCallback()
      }
    },
    async ['getCardshow'] ({commit}, id) {
      const { data: { data, code } } = await api.post('/v1/memberInfo',
        {openid: util.localItem.get('openid'), card_id: id})
      if (data && code === '200') {
        commit('setCardshow', data)
      }
    }
  },
  getters: {
    ['getCardList'] (state) {
      return state.list
    },
    ['getCardshow'] (state) {
      return state.show
    }
  }
}
